import 'package:flutter/material.dart';

class GridViewPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("GridView"),
      ),
      body: new GridListView(),
    );
  }
}

class GridListView extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _GridListView();
  }
}

class _GridListView extends State<GridListView> {
  List<IconData> _icons = []; //保存Icon数据
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return GridView.builder(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
      crossAxisCount: 3,//每行三列
        childAspectRatio: 1.0,//显示区域宽高相等
    ),
      itemCount: _icons.length,
      itemBuilder: (BuildContext context,int index){
        //如果显示到最后一个并且Icon总数小于200时继续获取数据
        if(index==_icons.length-1&&_icons.length<200){
          _retrieveIcons();
        }
        return Icon(_icons[index]);
      });
  }

  //模拟异步获取数据
  void _retrieveIcons(){
    Future.delayed(Duration(milliseconds: 200)).then((e){
      setState(() {
        _icons.addAll([
          Icons.ac_unit,
          Icons.airport_shuttle,
          Icons.all_inclusive,
          Icons.beach_access,Icons.cake,
          Icons.free_breakfast

        ]);
      });
    });
  }

}
